﻿using BCL.BaseLib.Basics;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace BCL.BaseLib
{
    [Serializable]
    public class FunctionEntity
    {
        private IFunctionControl control;
        private ICustom custom;
        private List<string> assemblyNames;
        public string AppId { get; set; }
        private Stream image;
        /// <summary>
        /// 
        /// </summary>
        public string AppCode { get; set; }

        /// <summary>
        /// 父编号
        /// </summary>
        public string Parent { get; set; }
        /// <summary>
        /// 要显示的标题
        /// </summary>
        public string Title { get; set; }

        public string ImagePath { get; set; }
        /// <summary>
        /// 图标
        /// </summary>
        [System.Xml.Serialization.SoapIgnore]
        [Newtonsoft.Json.JsonIgnore]
        public Stream Image {
            get
            {
                if (image == null)
                    image=Tool.Extend.ImageExtend.GetFileStream(ImagePath);
                return image;
            }
            set { image = value; } }

        /// <summary>
        /// 提示语
        /// </summary>
        public FunctionTip Tip { get; set; }

        /// <summary>
        /// 是否隐藏 1是0否
        /// by ZhaoHuan 2019/1/28
        /// </summary>
        public int IsHide { get; set; }

        /// <summary>
        /// 子功能
        /// </summary>
        public List<FunctionEntity> Items { get; set; }

        /// <summary>
        /// 控件所在DLL与类信息
        /// </summary>
        public ClassInfo ClassInfo { get; set; }

        /// <summary>
        /// 菜单执行类型 
        /// </summary>
        public FunctionType FunctionType { get; set; }



        /// <summary>
        /// 要执行的自定义的函数对象
        /// </summary>
        [System.Xml.Serialization.SoapIgnore]
        [Newtonsoft.Json.JsonIgnore]
        public ICustom Custom
        {
            get
            {
                if (FunctionType != FunctionType.Function)
                    return null;
                if (custom == null && ClassInfo != null)
                {
                    Assembly ass = GetAssembly(ClassInfo);
                    if (ass != null)
                        custom = ass.CreateInstance(ClassInfo.ClassFillName) as ICustom;
                }
                return custom;
            }
            set { custom = value; }
        }

        /// <summary>
        /// 要加载或打开的控件
        /// </summary>
        [System.Xml.Serialization.SoapIgnore]
        [Newtonsoft.Json.JsonIgnore]
        public Basics.IFunctionControl Control
        {
            get
            {
                if (FunctionType != FunctionType.Control)
                    return null;
                if ((control == null && ClassInfo != null) || (control != null && ClassInfo != null))
                {
                    if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + ClassInfo.DLLName))
                        return null;
                    Assembly ass = GetAssembly(ClassInfo);
                    //如果类具有一个参数的构造,优先使用
                    ConstructorInfo constructor = ass.GetType(ClassInfo.ClassFillName).GetConstructor(new Type[] { typeof(FunctionEntity) });
                    if (constructor != null)
                        control = constructor.Invoke(new object[] { this }) as IFunctionControl;
                    else
                    {
                        control = ass.CreateInstance(ClassInfo.ClassFillName) as IFunctionControl;
                        if (control != null)
                            control.Function = this;
                    }
                }
                return control;
            }
            set { control = value; }
        }

        /// <summary>
        /// 加载时是否执行
        /// </summary>
        public bool StartLoad { get; set; }
        /// <summary>
        /// 设置此项为true,主窗体加载此项为首页,注:只有在本菜单为FunctionType.Control是生效
        /// </summary>
        public bool Index { get; set; }

        /// <summary>
        /// 菜单项类型  如FucntionItemType.Group|FucntionItemType.Item
        /// </summary>
        public FunctionItemType ItemType
        {
            get;
            set;
        }

        /// <summary>
        /// 快捷键
        /// </summary>
        public Keys HotKey
        {
            get;
            set;
        }

        /// <summary>
        /// 是否创建菜单项 
        /// </summary>
        public bool IsCreate
        {
            get;
            set;
        }

        /// <summary>
        /// 刷新数据参数
        /// </summary>
        public object ItemParamter
        {
            get;
            set;
        }

        /// <summary>
        /// 是否重新加载页面
        /// </summary>
        public bool IsReload
        {
            get;
            set;
        }
        /// <summary>
        /// 备注
        /// </summary>
        public string Remarks
        {
            get;
            set;
        }

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool Enabled { get; set; }

        public FunctionEntity()
        {
            assemblyNames = new List<string>();
            ItemType = FunctionItemType.Group;
            this.FunctionType = FunctionType.Control;
            IsCreate = true;
            Enabled = true;
        }

        private Assembly GetAssembly(ClassInfo info)
        {
            if (assemblyNames.Any(o => o == info.DLLName))
            {
                Assembly[] a = AppDomain.CurrentDomain.GetAssemblies();
                foreach (var item in a)
                    if (!item.IsDynamic && item.Location == info.DLLName)
                        return item;
            }
            Assembly ass = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + ClassInfo.DLLName);
            if (ass != null)
                assemblyNames.Add(info.DLLName);
            return ass;
        }
    }

    public class ClassInfo
    {
        public string DLLName { get; set; }
        public string ClassFillName { get; set; }
    }

    public class FunctionTip
    {
        public string Title { get; set; }
        public string Text { get; set; }


    }

    /// <summary>
    /// 菜单执行方法的类型
    /// </summary>
    public enum FunctionType
    {
        /// <summary>
        /// 这是一个应用
        /// </summary>
        App=1,
        /// <summary>
        /// 创建或加载控件
        /// </summary>
        Control = 2,

        /// <summary>
        /// 执行自定义函数
        /// </summary>
        Function = 4,

        /// <summary>
        /// 表示当前节点只是一个权限点 
        /// </summary>
        Authorization = 8
    }

    /// <summary>
    /// 菜单项类型
    /// </summary>
    public enum FunctionItemType
    {
        /// <summary>
        /// 分组
        /// </summary>
        Group = 1,
        /// <summary>
        /// 项
        /// </summary>
        Item = 2
    }
}
